package pokerbot.services.impl;

import java.util.ArrayList;
import java.util.List;
import pokerbot.entities.Card;
import pokerbot.entities.Hand;
import pokerbot.entities.Player;
import pokerbot.Constants.CardPositions;
import pokerbot.entities.Table;
import pokerbot.services.ICreatingObjectsService;
import pokerbot.services.IRecognizingService;

public class CreatingObjectsServiceImpl implements ICreatingObjectsService {
    
    private static CreatingObjectsServiceImpl instance = null;
    private IRecognizingService recognizeService = null;

    private CreatingObjectsServiceImpl(IRecognizingService recognizeService) {
        this.recognizeService = recognizeService;
    }

    public static ICreatingObjectsService getInstance(IRecognizingService recognizeService) {
        if (instance == null) {
            instance = new CreatingObjectsServiceImpl(recognizeService);
        }
        return instance;
    }
    
    @Override
    public List<Player> createPlayers() {
        int playersCount = recognizeService.recognizePlayersCount();
        List<Player> players = new ArrayList<>();
        for (int i = 0; i < playersCount; i++) {
            players.add(createPlayer(i));
        }
        return players;
    }
    
    private Player createPlayer(int position) {
        return recognizeService.recognizePlayer(position);
    }
    
    @Override
    public Hand createHand() {
        return new Hand(createCard(CardPositions.BOTS_CARD_1), 
                createCard(CardPositions.BOTS_CARD_2));
    }
    
    @Override
    public Card createCard(CardPositions cardPosition) {
        return recognizeService.recognizeCard(cardPosition);
    }
    
    @Override
    public Table createTable() {
        Table table = new Table();
        table.addPlayers(createPlayers());
        return table;
    }
    
}
